/**
 * Description <br>
 *
 * @author krzysztof.ryk@solsoft.pl
 * @created 2010-01-21
 * @version $Revision:$ by $Author: $
 */
package com.torfox.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import lombok.Data;

import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Based object for ORM
 */
@MappedSuperclass
@Data
public abstract class DataObject implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	protected boolean equalsStringValue(final Object o1, final Object o2)
	{
		if (o1 == null && o2 == null)
		{
			return true;
		}
		return o1 != null && o2 != null && o1.toString().equals(o2.toString());
	}

	protected boolean equalsObjects(final Object o1, final Object o2)
	{
		if (o1 == null && o2 == null)
		{
			return true;
		}
		return o1 != null && o2 != null && o1.equals(o2);
	}

	protected boolean equalsDataObjects(final DataObject d1, final DataObject d2)
	{
		if (d1 == null && d2 == null)
		{
			return true;
		}
		if (d1 == null || d2 == null)
		{
			return false;
		}
		return d1.deepEquals(d2);
	}

	public boolean deepEquals(final DataObject obj)
	{
		return equals(obj);
	}

	protected <T extends DataObject> boolean equalsLists(final List<T> l1, final List<T> l2)
	{
		if (l1 == null && l2 == null)
		{
			return true;
		}
		if (l1 == null || l2 == null)
		{
			return false;
		}
		if (l1.size() != l2.size())
		{
			return false;
		}
		for (final T t1 : l1)
		{
			final int index = l2.indexOf(t1);
			if (index < 0)
			{
				return false;
			}
			final T t2 = l2.get(index);
			if (!t1.deepEquals(t2))
			{
				return false;
			}
		}
		return true;
	}


	@Override
    public int hashCode()
	{
		if( id != null)
        {
	        return new HashCodeBuilder().append(id).toHashCode();
        }
		return super.hashCode();
	}

	public boolean isNew()
	{
		return id == null;
	}
}
